// 比较两个数组 返回新增 删除 修改
import { useEnumeration } from '@/store/useEnumeration'
const COLUMN_LIST = [
  {
    label: '评审项名称',
    prop: 'name'
  },
  {
    label: '备注',
    prop: 'description'
  }
]

const FILTER_FORM_ITEM = [
  {
    key: 'name',
    value: '',
    name: '评审项名称',
    formType: 'input',
    verificationData: '',
    elementConfig: {
      textareaWidth: '550px'
    }
  }
]

const INCLUDE_CODE = {
  key: 'includeCode',
  value: '',
  name: '是否纳入总分',
  formType: 'select',
  verificationData: '必填',
  elementConfig: {
    textareaWidth: '550px',
    options: []
  }
}
// 新增评审项
const ADD_FORM_ITEM = [
  {
    key: 'name',
    value: '',
    name: '评审项名称',
    formType: 'input',
    verificationData: '必填',
    elementConfig: {
      textareaWidth: '550px'
    }
  },
  {
    key: 'markModeCode',
    value: null,
    name: '打分方式',
    formType: 'select',
    verificationData: '必填',
    elementConfig: {
      textareaWidth: '550px',
      options: []
    }
  },
  {
    key: 'scoreCollectWayCode',
    value: null,
    name: '评分点汇总方式',
    formType: 'select',
    verificationData: '必填',
    elementConfig: {
      textareaWidth: '550px',
      options: []
    }
  },
  {
    key: 'darkMarkCode',
    value: null,
    name: '是否暗标',
    formType: 'select',
    verificationData: '必填',
    elementConfig: {
      textareaWidth: '550px',
      options: []
    }
  },
  {
    key: 'includeCode',
    value: '',
    name: '是否纳入总分',
    formType: 'select',
    verificationData: '必填',
    elementConfig: {
      textareaWidth: '550px',
      options: []
    }
  },
  {
    key: 'description',
    value: '',
    name: '备注',
    formType: 'textarea',
    verificationData: '',
    elementConfig: {
      rows: 3,
      textareaWidth: '1110px'
    }
  }
]
const PROJECT_ADD_FORM_ITEM = [
  ...ADD_FORM_ITEM,
  {
    key: 'flowOrderCode',
    value: '',
    name: '流程顺序',
    formType: 'select',
    verificationData: '必填',
    elementConfig: {
      textareaWidth: '550px',
      options: []
    }
  },
  {
    key: 'orderNo',
    value: null,
    name: '',
    formType: 'empty',
    verificationData: '',
    elementConfig: {}
  }
]
// 新增评分点
const SCORE_POINT_FORM_ITEM = [
  {
    key: 'name',
    value: '',
    name: '评分点名称',
    formType: 'input',
    verificationData: '必填',
    elementConfig: {
      textareaWidth: '1130px'
    }
  },
  {
    key: 'standard',
    value: '',
    name: '评审标准',
    formType: 'input',
    verificationData: '',
    elementConfig: {
      rows: 3,
      type: 'textarea',
      textareaWidth: '1130px'
    }
  },
  {
    key: 'maxScore', // 3
    value: null,
    name: '最高分',
    formType: 'input',
    verificationData: '最高分',
    elementConfig: {
      textareaWidth: '550px'
    }
  },
  {
    key: 'minScore', // 4
    value: null,
    name: '最低分',
    formType: 'input',
    verificationData: '最低分',
    elementConfig: {
      textareaWidth: '550px'
    }
  },
  {
    key: 'score', // 5
    value: '',
    name: '打分值',
    formType: 'input',
    verificationData: '打分值',
    elementConfig: {
      placeholder: '请填写正确的值，例如：2/4/6/8',
      textareaWidth: '550px'
    }
  },
  {
    key: 'typeCode',
    value: '',
    name: '打分类型',
    formType: 'select',
    verificationData: '必填',
    elementConfig: {
      textareaWidth: '550px',
      options: []
    }
  },
  {
    key: 'itemId',
    value: '',
    name: '',
    formType: 'empty',
    verificationData: '',
    elementConfig: {}
  },
  {
    key: 'orderNo',
    value: null,
    name: '',
    formType: 'empty',
    verificationData: '',
    elementConfig: {}
  },
  {
    key: 'id',
    value: null,
    name: '',
    formType: 'empty',
    verificationData: '',
    elementConfig: {}
  },
  {
    key: 'parentNo',
    value: null,
    name: '',
    formType: 'empty',
    verificationData: '',
    elementConfig: {}
  }
]
// 新增评分点 - 符合性打分
const SCORE_POINT_FORM_ITEM_2 = [
  {
    key: 'name',
    value: '',
    name: '评分点名称',
    formType: 'input',
    verificationData: '必填',
    elementConfig: {
      textareaWidth: '1130px'
    }
  },
  {
    key: 'standard',
    value: '',
    name: '评审标准',
    formType: 'input',
    verificationData: '',
    elementConfig: {
      rows: 3,
      type: 'textarea',
      textareaWidth: '1130px'
    }
  },
  {
    key: 'typeCode',
    value: '',
    name: '打分类型',
    formType: 'select',
    verificationData: '必填',
    elementConfig: {
      textareaWidth: '1130px',
      options: []
    }
  },
  {
    key: 'itemId',
    value: '',
    name: '',
    formType: 'empty',
    verificationData: '',
    elementConfig: {}
  },
  {
    key: 'orderNo',
    value: null,
    name: '',
    formType: 'empty',
    verificationData: '',
    elementConfig: {}
  },
  {
    key: 'id',
    value: null,
    name: '',
    formType: 'empty',
    verificationData: '',
    elementConfig: {}
  },
  {
    key: 'parentNo',
    value: null,
    name: '',
    formType: 'empty',
    verificationData: '',
    elementConfig: {}
  }
]

const CALCULATION_FORMULA = [
  {
    key: 'calculteCode',
    value: '',
    name: '计算公式',
    formType: 'select',
    verificationData: '必填',
    elementConfig: {
      textareaWidth: '550px',
      options: []
    }
  }
]

// 添加子级标志 (后端需要)
const addChildrenFlag = (data: any) => {
  const handle = (items: any[]) => {
    items.forEach((item: any) => {
      if (item.children && item.children.length > 0) {
        item.hasChild = true
        handle(item.children)
      } else {
        item.hasChild = false
      }
    })
  }
  handle(data)
  return data
}
// 处理数据扁平 isApi 接口数据还是页面数据
const handlePointInfoViewFlat = (res: any) => {
  const result: any[] = []
  const handle = (data: any) => {
    data.forEach((item: any) => {
      const { formData } = item
      const temp = {
        orderNo: formData.orderNo,
        name: formData.name,
        standard: formData.standard,
        maxScore: formData.maxScore,
        minScore: formData.minScore,
        score: formData.score,
        typeCode: formData.typeCode,
        typeName: formData.typeName,
        calculteCode: formData.calculteCode,
        calculteName: formData.calculteName,
        jsonData: formData.jsonData,
        itemId: formData.itemId,
        id: formData.id,
        parentNo: item.parentNo,
        hasChild: item.hasChild, // 是否有子级
        VUE_DATA: item.VUE_DATA // 是否是页面数据
      }
      result.push(temp)
      if (item.children) {
        handle(item.children)
      }
    })
  }
  handle(res)
  return result
}

// 添加打分类型 的 code
const addMarkScoreTypeEnum = (list: any[]) => {
  const { markScoreTypeEnum } = useEnumeration()
  const tempMap: any = {}
  markScoreTypeEnum.forEach((item: any) => {
    tempMap[item.key] = item.value
  })
  return list.map((item) => {
    item.typeName = tempMap[item.typeCode]
    return item
  })
}
// 接口数据 三个list 新增 删除 修改
const compareArrays = (original: any[], current: any[]) => {
  const addList = addMarkScoreTypeEnum(current.filter((item) => item.VUE_DATA))
  const updateList = addMarkScoreTypeEnum(current.filter((item) => !item.VUE_DATA))
  const deleteList: any[] = []
  original.forEach((item) => {
    const findItem = current.find((i) => i.id === item.id)
    if (!findItem) {
      deleteList.push(item.id)
    }
  })
  return { addList, deleteList, updateList }
}
// 批量验证表单
const judgeFormValidate = async (formRef: any) => {
  try {
    const res = await Promise.all(formRef.map((item) => item.formValidate()))
    return res.every((item) => item)
  } catch (error) {
    console.log('🐷 🐽 judgeFormValidate 🐽 error👉👉👉', error)
    return false
  }
}

/**
 * 判断最高分是否正确 子集的maxScore总和等于父集的maxScore
 * @param arr 评分点列表
 * @returns 是否正确
 */
const judgeMaxScore = (arr: any) => {
  const validateNode = (node: any): { isValid: boolean; errorNodes: string[] } => {
    const { formData, children } = node
    const errorNodes: string[] = []

    if (!children || children.length === 0) {
      return { isValid: true, errorNodes: [] }
    }

    // 计算子节点的 maxScore 总和
    const childrenMaxScoreSum = children.reduce((sum: number, child: any) => {
      return sum + Number(child.formData.maxScore ?? 0)
    }, 0)

    // 比较父节点的 maxScore 是否等于子节点 maxScore 之和
    const isEqual = Math.abs(childrenMaxScoreSum - Number(formData.maxScore ?? 0)) < 0.001

    if (!isEqual) {
      errorNodes.push(`${formData.name}（父节点分数：${formData.maxScore}，子节点分数之和：${childrenMaxScoreSum}）`)
    }

    // 递归检查所有子节点
    children.forEach((child: any) => {
      const childResult = validateNode(child)
      errorNodes.push(...childResult.errorNodes)
    })

    return {
      isValid: isEqual && errorNodes.length === 0,
      errorNodes
    }
  }

  // 检查所有顶级节点
  const results = arr.map((node: any) => validateNode(node))
  const allErrorNodes = results.flatMap((result) => result.errorNodes)

  return {
    isValid: allErrorNodes.length === 0,
    errorNodes: allErrorNodes
  }
}
// 工程方法1
const ENGINEERING_METHOD =
  'A值：以有效投标文件的评标价算术平均值为A 【当有效投标文件＞7家时，去掉最高20%和最低20%(四舍五入取整)后进行平均; 当 4 ≦ 有效投标文件 ＜ 7家时，剔除最高报价(最高报价相同的均剔除)后进行算术平均; 当有效投标文件 ≦ 4家时，则次低报价作为投标平均价A】'
export {
  judgeMaxScore,
  addChildrenFlag,
  judgeFormValidate,
  compareArrays,
  handlePointInfoViewFlat,
  PROJECT_ADD_FORM_ITEM,
  INCLUDE_CODE,
  ADD_FORM_ITEM,
  SCORE_POINT_FORM_ITEM,
  SCORE_POINT_FORM_ITEM_2,
  CALCULATION_FORMULA,
  COLUMN_LIST,
  FILTER_FORM_ITEM,
  ENGINEERING_METHOD
}
